package com.wl.shixun3.controller;

import com.wl.shixun3.entity.User;
import com.wl.shixun3.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 注册控制器
 */
@WebServlet("/shixun3/register")
public class RegisterController extends HttpServlet {
    private UserService userService;

    @Override
    public void init() throws ServletException {
        userService = new UserService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 转发到注册页面
        request.getRequestDispatcher("/shixun3/register.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 获取表单参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String securityQuestion = request.getParameter("securityQuestion");
        String securityAnswer = request.getParameter("securityAnswer");

        // 参数验证
        if (username == null || username.trim().isEmpty() ||
            password == null || password.trim().isEmpty() ||
            securityQuestion == null || securityQuestion.trim().isEmpty() ||
            securityAnswer == null || securityAnswer.trim().isEmpty()) {
            request.setAttribute("errorMessage", "所有字段都不能为空！");
            request.getRequestDispatcher("/shixun3/register.jsp").forward(request, response);
            return;
        }

        // 检查用户名是否已存在
        if (userService.checkUsernameExists(username)) {
            request.setAttribute("errorMessage", "用户名已存在，请选择其他用户名！");
            request.getRequestDispatcher("/shixun3/register.jsp").forward(request, response);
            return;
        }

        // 创建新用户
        User newUser = new User(username, password, securityQuestion, securityAnswer);
        
        try {
            boolean success = userService.registerUser(newUser);
            
            if (success) {
                // 注册成功
                request.setAttribute("successMessage", "恭喜您 " + username + " 注册成功！请登录。");
                request.setAttribute("newUser", newUser);
                request.getRequestDispatcher("/shixun3/registerSuccess.jsp").forward(request, response);
            } else {
                // 注册失败
                request.setAttribute("errorMessage", "注册失败，请稍后再试！");
                request.getRequestDispatcher("/shixun3/register.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "注册失败，请稍后再试！");
            request.getRequestDispatcher("/shixun3/register.jsp").forward(request, response);
        }
    }
}

